package com.design.pattern.state;


import com.alibaba.fastjson.JSONObject;
import com.design.pattern.state.domain.ChatRoom;
import com.design.pattern.state.domain.User;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Client {
    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        boolean logger=false;
        if (args.length>0){
            logger="true".equals(args[0].trim().toLowerCase())?Boolean.TRUE:Boolean.FALSE;
        }
        List<ChatRoom> rooms=createRooms(10);
        List<User> users=createUsers(10);
        for (User user:users){
            for (ChatRoom room:rooms){
                if (logger){
                    System.out.println(JSONObject.toJSONString(room));
                    System.out.println(JSONObject.toJSONString(user));
                }
                new UserRoomContext(room,user).login();

            }
        }

    }

    /**
     * 创建指定数量的房间
     * @param number 数量
     * @return
     */
    public static List<ChatRoom> createRooms(Integer number){
        List<ChatRoom> rooms=new ArrayList<>();
        for(int i=0;i<number;i++){
            ChatRoom room=new ChatRoom();
            room.setId(UUID.randomUUID().toString());
            room.setName("房间"+UUID.randomUUID().toString().substring(0,3));
            room.setHasPassword(((int)(Math.random()*2))>0?true:false);
            room.setVip(((int)(Math.random()*2))>0?true:false);
            if (room.getHasPassword()){
                room.setPassword(UUID.randomUUID().toString().substring(0,6));
            }
            rooms.add(room);
        }
        return rooms;
    }

    /**
     * 创建指定数量的用户
     * @param number 数量
     * @return
     */
    public static List<User> createUsers(Integer number){
        List<User> users=new ArrayList<>();
        for (int i=0;i<number;i++){
            User user=new User();
            user.setId(UUID.randomUUID().toString());
            user.setVip(((int)(Math.random()*2))>0?true:false);
            user.setName(createName());
            users.add(user);
        }
        return users;
    }

    /**
     * 生成用户名
     * @return
     */
    public static String createName(){
        String[] first=new String[]{"赵","钱","孙","李","周"};
        String[] last=new String[]{"强","丽","盈","梅","轩"};
        StringBuffer stringBuffer=new StringBuffer();
        return  stringBuffer.append(first[(int)(Math.random()*first.length)]).append(last[(int)(Math.random()*last.length)]).toString();
    }
}
